package com.ivc.info.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cs.framework.common.util.CommonUtils;
import com.ivc.info.dto.StockInOutDto;
import com.ivc.info.dto.base.StockDetailDto;
import com.ivc.info.dto.base.StockInfoDto;
import com.ivc.info.service.ProductService;
import com.ivc.info.service.StockDetailService;
import com.ivc.info.service.StockInfoService;
import com.ivc.info.service.StockManageService;
import com.ivc.info.service.SupplierService;

/**
 * 库存管理
 * 
 * @author hedongzhou
 * 
 */
@Service("stockManageService")
public class StockManageServiceImpl implements StockManageService {

	@Resource
	private StockInfoService stockInfoService;

	@Resource
	private StockDetailService stockDetailService;

	@Resource
	private ProductService productService;

	@Resource
	private SupplierService supplierService;

	/**
	 * 创建一个商品库存
	 * 
	 * @param productNo
	 *            商品编号
	 * @return
	 */
	@Transactional
	public StockInfoDto createStock(String productNo) {
		StockInfoDto stockInfoDto = new StockInfoDto();
		stockInfoDto.setProductNo(productNo);

		if (stockInfoService.count(stockInfoDto) > 0) {
			CommonUtils.throwRun("该商品的库存已存在");
		}

		stockInfoDto.setQuantity(0);

		return stockInfoService.save(stockInfoDto);
	}

	/**
	 * 入库/出库
	 * 
	 * @param stockInOutDto
	 */
	@Transactional
	public void inOrOut(StockInOutDto stockInOutDto) {
		CommonUtils.checkNotNull(stockInOutDto);

		String productName = null;
		String supplierName = null;

		StockInfoDto stockInfoDto = stockInfoService.getByProductNo(stockInOutDto.getProductNo());
		if (stockInfoDto == null) {
			productName = productService.getProductName(stockInOutDto.getProductNo());

			CommonUtils.throwRun("商品【" + productName + "】的库存不存在");
		}

		if (stockInOutDto.getQuantity() < 0) {
			Integer quantity = Math.abs(stockInOutDto.getQuantity());

			if (quantity > stockInfoDto.getQuantity()) {
				productName = productService.getProductName(stockInOutDto.getProductNo());

				CommonUtils.throwRun("商品【" + productName + "】本次出库数量【" + quantity + "】超过了库存总量【"
						+ stockInfoDto.getQuantity() + "】");
			}

			int sum = stockDetailService.sumQuanBySupplier(stockInfoDto.getStockNo(), stockInOutDto.getSupplierNo());
			if (quantity > sum) {
				supplierName = supplierService.getSupplierName(stockInOutDto.getSupplierNo());
				productName = productService.getProductName(stockInOutDto.getProductNo());

				CommonUtils.throwRun("从供应商【" + supplierName + "】中购买的商品【" + productName + "】的库存数量为【" + sum
						+ "】，不足本次出库数量【" + quantity + "】");
			}
		}

		stockInfoDto.setQuantity(stockInfoDto.getQuantity() + stockInOutDto.getQuantity());
		stockInfoService.update(stockInfoDto);

		StockDetailDto stockDetailDto = new StockDetailDto();
		CommonUtils.copyBoValue(stockDetailDto, stockInOutDto);
		stockDetailDto.setStockNo(stockInfoDto.getStockNo());

		stockDetailService.save(stockDetailDto);
	}
}
